package io.renren.modules.myCourse.controller;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.course.entity.CourseBodyEntity;
import io.renren.modules.course.service.CourseBodyService;
import io.renren.modules.myCourse.entity.DelectPicEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.myCourse.entity.ChooseTestEntity;
import io.renren.modules.myCourse.service.ChooseTestService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author zlb
 * @email 316682546@qq.com
 * @date 2019-02-28 13:53:38
 */
@RestController
@RequestMapping("myCourse/choosetest")
public class ChooseTestController {
    @Autowired
    private ChooseTestService chooseTestService;
    @Autowired
    private CourseBodyService courseBodyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        CourseBodyEntity courseBodyEntity=courseBodyService.selectById((Serializable) params.get("courseBodyId"));
//        if(courseBodyEntity.getParentId()==null){
//            params.put("chapterId",courseBodyEntity.getId());
//        }else {
//            params.put("sectionId",courseBodyEntity.getId());
//        }
        PageUtils page = chooseTestService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        ChooseTestEntity chooseTest = chooseTestService.selectById(id);

        return R.ok().put("chooseTest", chooseTest);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save",consumes = "application/json;charset=utf-8")
    public R save(@RequestBody ChooseTestEntity chooseTest){
        chooseTestService.insert(chooseTest);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update",consumes = "application/json;charset=utf-8")
    public R update(@RequestBody ChooseTestEntity chooseTest){
        ValidatorUtils.validateEntity(chooseTest);
        chooseTestService.updateAllColumnById(chooseTest);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer ids){
        chooseTestService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 删除题干图片
     */
    @RequestMapping("/delquestonContentImg")
    public R delquestonContentImg(@RequestBody DelectPicEntity delectPicEntity){
        int id = Integer.valueOf(delectPicEntity.getIid().toString());
        int type = Integer.valueOf(delectPicEntity.getTypee().toString());
        if(Integer.valueOf(id) == null){
            return R.error(102,"请选择删除图片的所属测试题");
        }
        if(type == 0){
            //删除题干图片
            chooseTestService.delquestonContentImg(id);
        }else if(type == 1){
            //删除选项一图片
            chooseTestService.delquestonOneImg(id);
        }
        else if(type == 2){
            //删除选项二图片
            chooseTestService.delquestonTwoImg(id);
        }else if(type == 3){
            //删除选项三图片
            chooseTestService.delquestonThreeImg(id);
        }else if(type == 4){
            //删除选项四图片
            chooseTestService.delquestonFourImg(id);
        }else if(type == 5){
            //删除解答图片
            chooseTestService.delquestonAlaisImg(id);
        }
        return R.ok();
    }

}
